<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="searchForm" :inline="true" :model="searchFilter">
        <el-form-item label="权限ID" prop="id"><el-input v-model="searchFilter.id" clearable placeholder="请输入权限ID" /></el-form-item>
        <el-form-item label="权限规则" prop="name"><el-input v-model="searchFilter.name" clearable placeholder="请输入权限规则" /></el-form-item>
        <el-form-item label="权限规则" prop="display_name"><el-input v-model="searchFilter.display_name" clearable placeholder="请输入权限名称" /></el-form-item>
        <el-form-item>
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
          <el-button class="filter-item" type="primary" icon="el-icon-circle-close" @click="clear">清除</el-button>
          <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;" empty-text>
      <el-table-column prop="id" label="权限ID" align="center" width="80" />
      <el-table-column prop="display_name" label="权限名称" width="180" />
      <el-table-column prop="name" label="权限规则" width="180" />
      <el-table-column prop="created_at" label="创建时间" />
      <el-table-column prop="updated_at" label="更新时间" />
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination :total="total" :page.sync="searchFilter.current_page" :limit.sync="searchFilter.per_page" @pagination="getList" />

    <el-dialog :title="dialogTitleMap[dialogStatus]" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" class="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="width: 500px; margin-left:50px;">
        <el-form-item label="权限名称" prop="display_name" label-width="80px"><el-input v-model="temp.display_name" maxlength="100" placeholder="请输入权限名称" /></el-form-item>
        <el-form-item label="权限规则" prop="name" label-width="80px">
          <el-input v-model="temp.name" maxlength="100" placeholder="权限规则是后端的路由name,例如permission.list" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">提交</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { permissionList, createPermission, updatePermission, deletePermission } from '@/api/permission'
import Pagination from '@/components/Pagination'
export default {
  name: 'PermissionList',
  components: { Pagination },
  data() {
    return {
      listLoading: true,
      total: 0,
      list: [],
      searchFilter: {
        id: '',
        display_name: '',
        name: '',
        current_page: 1,
        per_page: 20
      },
      dialogFormVisible: false,
      dialogStatus: '',
      dialogTitleMap: {
        update: '编辑',
        create: '创建'
      },
      temp: {
        id: 0,
        name: '',
        display_name: ''
      },
      rules: {
        display_name: [
          { required: true, message: '请输入权限名称', trigger: ['blur', 'change'] },
          { min: 2, message: '权限名称不能少于2位', trigger: ['blur', 'change'] },
          { max: 40, message: '权限名称不能大于40位', trigger: ['blur', 'change'] }
        ],
        name: [
          { required: true, message: '请输入权限规则', trigger: ['blur', 'change'] },
          { min: 2, message: '权限规则不能少于2位', trigger: ['blur', 'change'] },
          { max: 255, message: '权限规则不能大于255位', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    resetTemp() {
      this.temp = {
        id: 0,
        name: '',
        display_name: ''
      }
    },
    getList() {
      this.listLoading = true
      permissionList(this.searchFilter).then(response => {
        this.list = response.data.list
        this.searchFilter.current_page = response.data.pagination.current_page
        this.searchFilter.per_page = response.data.pagination.per_page
        this.total = response.data.pagination.total
        this.listLoading = false
      })
    },
    search() {
      this.getList(this.searchFilter)
    },
    clear() {
      this.$refs['searchForm'].resetFields()
      this.getList(this.searchFilter)
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePermission(row).then(res => {
          const index = this.list.findIndex(v => v.id === row.id)
          this.list.splice(index, 1)
          this.$notify({
            title: '成功',
            message: res.message,
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createPermission(this.temp).then(res => {
            this.list.unshift(res.data)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: res.message,
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatePermission(this.temp).then(res => {
            const index = this.list.findIndex(v => v.id === this.temp.id)
            this.list.splice(index, 1, res.data)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: res.message,
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>

<style scoped="scoped"></style>
